package DataAccessLayer;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;
import java.util.logging.Level;
import java.util.logging.Logger;

abstract class DataLayerBase {

    private Connection _mainConnection;
    private String username="";
    private String password="";
    private String driver="sun.jdbc.odbc.JdbcOdbcDriver";
    private String url="jdbc:odbc:MADB_BanHangDaCap";
//    private String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
//    private String url="jdbc:sqlserver://localhost:1433;databaseName=MADB_BanHangDaCap";
    boolean _isDisposed;

    public DataLayerBase() {
        InitClass();
    }

    public Connection getMainConnection() {
        return _mainConnection;
    }

    private void InitClass() {
        try {
            Class.forName(driver);
            _mainConnection = DriverManager.getConnection(url, username, password);

            _isDisposed = false;
        } catch (SQLException ex) {
            Logger.getLogger(DataLayerBase.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DataLayerBase.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void Dispose() {
        Dispose(true);
    }

    protected void Dispose(boolean bIsDisposing) {
        // Check to see if Dispose has already been called.
        if (!_isDisposed) {
            if (bIsDisposing) {
                try {
                    // Dispose managed resources.
                    getMainConnection().close();
                    _mainConnection = null;
                } catch (SQLException ex) {
                    Logger.getLogger(DataLayerBase.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        _isDisposed = true;
    }
}